import { Injectable, BadRequestException, SetMetadata } from '@nestjs/common';
import { BaseDao } from '@/common/baseDao/base.service';
import { Util } from '@/common/util/util.service';
import { ExerciseDto, MuscleDto } from './muscle.dto';
import { InsertOneResult, ObjectId } from 'mongodb';
import * as bcrypt from 'bcryptjs';
import Config from '@/config';
import { JwtService } from '@/common/jwt/jwt.service';

@Injectable()
export class MuscleService {
  constructor(
    private baseDao: BaseDao,
    private util: Util,
  ) { }
  async muscleUpdate(muscleDto: MuscleDto): Promise<any> {
    const { sort, title, parent_id, _id } = muscleDto;

    if (!_id) {
      throw new BadRequestException('id不能为空');
    }

    try {
      await this.baseDao.updateById({
        dbName: 'quick-muscle',
        id: _id,
        dataJson: {
          title,
          sort,
          parent_id,
        },
      });

      return {
        success: true,
      };
    } catch (error) {
      throw new BadRequestException(error);
    }
  }

  async muscleAdd(muscleDto: MuscleDto): Promise<any> {
    const { sort, title, parent_id } = muscleDto;

    try {
      await this.baseDao.add({
        dbName: 'quick-muscle',
        dataJson: {
          title,
          sort,
          parent_id,
        },
      });

      return {
        success: true,
      };
    } catch (error) {
      throw new BadRequestException(error);
    }
  }

  async getMuscleList({ pageIndex, pageSize, title, parent_id }): Promise<any> {
    const { _ } = this.util;

    let whereJson = {};
    if (title) {
      whereJson = {
        title: _.eq(title),
      };
    }
    if (parent_id) {
      whereJson = {
        parent_id: parent_id,
      };
    }
    return await this.baseDao.selectRows({
      dbName: 'quick-muscle',
      whereJson,
      sortArr: [{ name: 'sort', type: 'asc' }],
      pageIndex: Number(pageIndex),
      pageSize: Number(pageSize),
    });
  }

  async muscleDelete(ids: string[]): Promise<any> {
    const { _ } = this.util;

    let whereJson = {};
    if (ids && ids.length > 0) {
      whereJson = {
        _id: _.in(ids),
      };

      const data = await this.baseDao.selectRows({
        dbName: 'quick-muscle',
        whereJson: {
          parent_id: _.in(ids),
        },
        sortArr: [{ name: 'sort', type: 'asc' }],
        pageIndex: 1,
        pageSize: 10,
      });

      console.log('data', data)

      if (data.length > 0) {
        throw new BadRequestException('请先删除子级');
      }
    } else {
      throw new BadRequestException('ids不能为空');
    }


    return await this.baseDao.del({
      dbName: 'quick-muscle',
      whereJson
    });
  }
}

@Injectable()
export class ExerciseService {
  constructor(
    private baseDao: BaseDao,
    private util: Util,
  ) { }
  async exerciseUpdate(exerciseDto: ExerciseDto): Promise<any> {
    const { sort, title, description, image, muscle_id, _id } = exerciseDto;

    if (!_id) {
      throw new BadRequestException('id不能为空');
    }

    try {
      await this.baseDao.updateById({
        dbName: 'quick-exercise',
        id: _id,
        dataJson: {
          title,
          sort,
          muscle_id,
          description,
          image,
        },
      });

      return {
        success: true,
      };
    } catch (error) {
      throw new BadRequestException(error);
    }
  }

  async exerciseAdd(exerciseDto: ExerciseDto): Promise<any> {
    const { sort, title, description, image, muscle_id } = exerciseDto;

    try {
      await this.baseDao.add({
        dbName: 'quick-exercise',
        dataJson: {
          title,
          sort,
          muscle_id,
          description,
          image,
        },
      });

      return {
        success: true,
      };
    } catch (error) {
      throw new BadRequestException(error);
    }
  }

  async getExerciseList({
    pageIndex,
    pageSize,
    title,
    muscle_id,
  }): Promise<any> {
    const { _ } = this.util;

    let whereJson = {};
    if (title) {
      whereJson = {
        title: _.eq(title),
      };
    }
    if (muscle_id) {
      whereJson = {
        muscle_id: muscle_id,
      };
    }
    return await this.baseDao.selectRows({
      dbName: 'quick-exercise',
      whereJson,
      sortArr: [{ name: 'sort', type: 'asc' }],
      pageIndex: Number(pageIndex),
      pageSize: Number(pageSize),
    });
  }

  async exerciseDelete(ids: string[]): Promise<any> {
    const { _ } = this.util;

    let whereJson = {};
    if (ids && ids.length > 0) {
      whereJson = {
        _id: _.in(ids),
      };
    } else {
      throw new BadRequestException('ids不能为空');
    }

    return await this.baseDao.del({
      dbName: 'quick-exercise',
      whereJson,
    });
  }
}
